// LEC March 7, 2021
// Dyadic models for Tilly paper
// including both cumulative vars

// Should be run with runall.do which contains all labels for directories and files


cd $ROOT
cd $INTERMEDIATEDIR
use dyad_analysis2, clear
 
gen peaceXgrowthwara = (1-inc1)*llgrowthwara
gen peaceXgrowthpeacea = (1-inc1)*llgrowthpeacea
gen peaceXshrinkwarb = (1-inc1)*llshrinkwarb
gen peaceXshrinkpeaceb = (1-inc1)*llshrinkpeaceb

gen lrwara = l5.growthwara/(l5.llareaa)
gen lrXlr = lr * lr

gen peaceXllr = (1-inc1)*llr
gen initXllr = incinita1 * llr

replace llelevsda = 0 if llelevsda == .
replace llelevsdb = 0 if llelevsdb == .


label variable lnga "log terr. gain A"
label variable lneigh "neighbors AB"
label variable llnumneighsa "\\# neighbors A"
label variable llnumneighsb "\\# neighbors B"
label variable llrthreata "neigh. threat A"
label variable llrthreatb "neigh. threat B"
label variable lldist "log distance AB"
label variable inc1 "war AB"
label variable lr "rel. size A/AB"
label variable lrXlr "rel. size ^2"
label variable llr "rel. log size A/AB"
label variable inc1Xllr "war X rel. log size"
label variable peaceXllr "peace X rel. log size"
label variable llareaa "log size A"
label variable lcoastala "Coastal access A"
label variable lcoastalb "Coastal access B"
label variable llagea "log age A"
label variable llageb "log age B"
label variable llareab "log size B"
label variable llelevsda "log elev. SD A"
label variable llelevsdb "log elev SD B"



*label variable llrestpopa "log rest pop. A" // Variables not found
*label variable llrestpopb "log rest pop. b"


label variable llgrowthwara "cumul. war gains A"
label variable llgrowthpeacea "cumul. peace gains A"
label variable inc1Xgrowthwara "war X war gains A"
label variable inc1Xgrowthpeacea "war X peace gains A"

label variable peaceXgrowthwara "peace X war gains A"
label variable peaceXgrowthpeacea "peace X peace gains A"


label variable llshrinkwarb "cumul. war losses B"
label variable llshrinkpeaceb "cumul. peace losses B"
label variable inc1Xshrinkwarb "war X war losses B"
label variable inc1Xshrinkpeaceb "war X peace losses B"

label variable peaceXshrinkwarb "peace X war losses B"
label variable peaceXshrinkpeaceb "peace X peace losses B"

label variable lrgrowthwarab "cumul. war gains A/B"
label variable lrgrowthpeaceab "cumul. peace gains A/B"
label variable inc1Xgrowthwarab "war X war gains A/B"
label variable inc1Xgrowthpeaceab "war X peace gains A/B"

label variable lrshrinkwarba "cumul. war losses B/A"
label variable lrshrinkpeaceba "cumul. peace losses B/A"
label variable inc1Xshrinkwarba "war X war losses B/A"
label variable inc1Xshrinkpeaceba "war X peace losses B/A"

label variable incinita1 "A initiator"
label variable initXllr "A init. X rel. size"

label variable llgrowtha "cumul. gains A"
label variable llshrinkb "cumul. losses B"


// GENERATE TABLE A12
// MAIN DYADIC MODELS FOR PAPER WITH CENTENNIA DATA

eststo clear
global X "inc1Xgrowthwara peaceXgrowthwara inc1Xgrowthpeacea peaceXgrowthpeacea inc1Xshrinkwarb peaceXshrinkwarb inc1Xshrinkpeaceb peaceXshrinkpeaceb"
global X1 "inc1##c.llgrowthwara inc1##c.llgrowthpeacea inc1##c.llshrinkwarb inc1##c.llshrinkpeaceb"  // version for plots
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb"
global TIMEVARS "noeventyear evspline*"

reghdfe lnga0 inc1 llr  llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1
  
reghdfe lnga0 inc1 llr inc1Xllr   llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS, absorb(year) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m3

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS, absorb(year idd) cluster(idd2 ida idb)
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "Yes"
estimates store m4

cd $ROOT
cd $OUTPUTDIR
estimates save "dyads.ster", replace

esttab m1 m2 m3 m4 using "dyads_cent.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic analysis of gains of State A and losses of State B (Centennia data) \label{tab:dyad:cent}") ///
order (inc1 llr inc1Xllr $X  llareaa llareab $CONTROLS) ///
keep (inc1 llr inc1Xllr $X  llareaa llareab $CONTROLS) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")


	  
	  

// GENERATE TABLE A13
// DYADIC GAINS AND LOSSES BY CENTURY WITH CENTENNIA DATA

eststo clear
global X "inc1Xgrowthwara peaceXgrowthwara inc1Xgrowthpeacea peaceXgrowthpeacea inc1Xshrinkwarb peaceXshrinkwarb inc1Xshrinkpeaceb peaceXshrinkpeaceb"
global X1 "inc1##c.llgrowthwara inc1##c.llgrowthpeacea inc1##c.llshrinkwarb inc1##c.llshrinkpeaceb"  // version for plots
global CONTROLS "lldist lneigh lcoastala lcoastalb llagea llageb llnumneighsa llnumneighsb  llelevsda llelevsdb"
global TIMEVARS "noeventyear evspline*"

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year<=1590, absorb(year) cluster(idd2 ida idb)
estadd local period "1490-1590"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m1
  
reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year>=1595 & year<=1690, absorb(year) cluster(idd2 ida idb)
estadd local period "1595-1690"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m2

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year>=1695 & year<=1790, absorb(year) cluster(idd2 ida idb)
estadd local period "1695-1790"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m3

reghdfe lnga0 inc1 $X llareaa llareab $CONTROLS $TIMEVARS if year>=1795, absorb(year) cluster(idd2 ida idb)
estadd local period "1795-1915"
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local dyadfe "No"
estimates store m4

cd $ROOT
cd $OUTPUTDIR
estimates save "dyads.ster", replace

esttab m1 m2 m3 m4 using "dyads_4centururies_cent.tex", replace /// 
b(4) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
nogaps eqlabels(none) obslast nomtitles ///
title("Dyadic analysis of gains and losses by century (Centennia data) \label{tab:dyadmain}") ///
order (inc1 $X  llareaa llareab) /// $CONTROLS) ///
keep (inc1 $X  llareaa llareab) /// $CONTROLS) ///
 nonotes addnotes("Standard errors clustered on states and dyads in parentheses." /// 
 "$^{+}p<0.1$; $^{*}p<0.05$; $^{**}p<0.01.$; $^{***}p<0.001.$ ") ///
      scalars("period Period" "geocontrols Geo. Controls" "yearfe Year FE" "dyadfe Dyad FE")
	  
	  
	  
	  
